<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="牛号:">
              <a-input style="width: 150px" v-model:value.trim="where.cattleNo" placeholder="请输入牛号" allow-clear />
            </a-form-item>

            <a-form-item label="温度序列号:">
              <a-input style="width: 150px" v-model:value.trim="where.thermNo" placeholder="温度序列号" allow-clear />
            </a-form-item>

            <a-form-item label="牧场:" name="ranchId">
              <a-select
                show-search
                v-model:value="where.ranchId"
                placeholder="请选择所属牧场"
                style="width: 180px;"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                @change="selectRanch"
              >
                <a-select-option v-for="item in ranchList" :key="item.ranchId" >
                  {{ item.ranchName }}
                </a-select-option>
              </a-select>
            </a-form-item>


            <a-form-item label="牛舍:" name="oxtId">
              <a-select
                show-search
                v-model:value="where.oxtId"
                placeholder="请选择所属牛舍"
                style="width: 180px"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
              >
                <a-select-option v-for="item in oxtallList" :key="item.oxtallId" >
                  {{ item.oxtallName }}
                </a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item label="牛群:" name="herdId">
              <a-select
                show-search
                v-model:value="where.herdId"
                placeholder="请选择所属牛群"
                style="width: 120px"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
              >
                <a-select-option v-for="item in herdList" :key="item.herdId" >
                  {{ item.herdName }}
                </a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="cattleId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          v-model:selection="selection"
          :scroll="{ x: 'max-content' }"
        >
          <!-- table上边工具栏 -->
          <template #toolbar>
            <a-space>
              <a-button type="primary" @click="openEdit()">
                <template #icon>
                  <plus-outlined />
                </template>
                <span>新建</span>
              </a-button>
            </a-space>
          </template>

          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openEdit(record)">修改</a>
                <a-divider type="vertical" />
                <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
                <a-divider type="vertical" />
                <a-dropdown>
                  <a class="ant-dropdown-link" @click.prevent>
                    更多操作
                    <DownOutlined />
                  </a>
                  <template #overlay>
                    <a-menu>
                      <a-menu-item key="0">
                        <a @click="openTemplateDataDialog(record)">健康数据</a>
                      </a-menu-item>
                      <a-menu-item key="1">
                        <a @click="openDeviceDataDialog(record)">关联设备</a>
                      </a-menu-item>
                      <a-menu-item key="2">
                        <a @click="openSportDataDialog(record)">运动数据</a>
                      </a-menu-item>
                      <a-menu-item key="3">
                        <a @click="openLiedownDataDialog(record)">躺卧数据</a>
                      </a-menu-item>
                      <a-menu-item key="4">
                        <a @click="openEstrusDataDialog(record)">爬跨数据</a>
                      </a-menu-item>
                      <a-menu-item key="5">
                        <a @click="openStomachDataDialog(record)">胃丸监测数据</a>
                      </a-menu-item>
                    </a-menu>
                  </template>
                </a-dropdown>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <!-- 编辑弹窗 -->
    <cattle-manager-cattle-edit v-if="showEdit" v-model:visible="showEdit" :data="current" @done="reload" />
    <cattle-manager-cattlehealth v-if="showHealthData" v-model:visible="showHealthData" :data="current" @done="reload" />
    <cattle-manager-cattle-devicedata-modal v-if="showDeviceData" v-model:visible="showDeviceData" :data="current" @done="reload" />
    <cattle-manager-cattlesport-modal v-if="showSportData" v-model:visible="showSportData" :data="current" @done="reload" />
    <cattle-manager-cattle-linedown-modal  v-if="showLiedownData" v-model:visible="showLiedownData" :data="current" @done="reload" />
    <cattle-manager-cattle-estrus-modal v-if="showEstrusData" v-model:visible="showEstrusData" :data="current" @done="reload" />
    <CattleManagerCattleStomachModal  v-if="showStomachData" v-model:visible="showStomachData" :data="current" @done="reload" />
  </div>
</template>

<script>
import { PlusOutlined, DownOutlined } from '@ant-design/icons-vue';
import { CattleApi } from '@/api/property/cattlemanager/CattleApi';
import CattleManagerCattleEdit from './cattle-edit.vue';
import { message } from 'ant-design-vue';
import {RanchApi} from "@/api/property/RanchApi";
import {OxtallApi} from "@/api/property/OxtallApi";
import {CattleHerdApi} from "@/api/property/cattlemanager/CattleHerdApi";
import CattleManagerCattlehealth from './cattlehealthdata/health-modal.vue'
import CattleManagerCattleDevicedataModal from  './cattledevicedata/devicedata-modal.vue'
import CattleManagerCattlesportModal from  './cattlesport/sport-modal.vue'
import CattleManagerCattleLinedownModal from './liedown/liedown-modal.vue'
import CattleManagerCattleEstrusModal from './estrus/estrus-modal.vue'
import CattleManagerCattleStomachModal from './stomach/stomach-modal.vue'
import moment from 'moment'

export default {
  name: 'CattleManagerCattle',
  components: {
    PlusOutlined,
    CattleManagerCattleEdit,
    CattleManagerCattlehealth,
    CattleManagerCattleDevicedataModal,
    DownOutlined,
    CattleManagerCattlesportModal,
    CattleManagerCattleLinedownModal,
    CattleManagerCattleEstrusModal,
    CattleManagerCattleStomachModal
  },
  async mounted() {
    RanchApi.list({}).then(resp => {
      let _ranchList = (resp ? resp.data : null) || [];
      this.ranchList = _ranchList;
      if (_ranchList.length > 0) {
        OxtallApi.list({"orgId" : _ranchList[0].ranchId}).then(result => {
          this.oxtallList = (result ? result.data : null) || [];
        }).catch((err) => {
          console.log(err)
        });
      }
    });

    CattleHerdApi.list({}).then(result => {
      this.herdList = (result ? result.data : null) || [];
    }).catch((err) => {
      console.log(err)
    });
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '牛号',
          dataIndex: 'cattleNo',
          fixed: 'left'
        },
        {
          title: '温度计序列号',
          dataIndex: 'thermNo',
          fixed: 'left'
        },
        {
          title: '瘤胃温度℃',
          dataIndex: 'cattleRealtimeDataDTO',
          customRender: function (row) {
            if (row && row.value && row.value.bodyTemperature) {
              return row.value.bodyTemperature;
            } else {
              return '';
            }
          }
        },
        {
          title: '瘤胃温度采集时间',
          dataIndex: 'cattleRealtimeDataDTO',
          customRender: function (row) {
            if (row && row.value && row.value.temperatureTime) {
              return row.value.temperatureTime;
            } else {
              return '';
            }
          }
        },
        {
          title: '呼吸(次/min)',
          dataIndex: 'cattleRealtimeDataDTO',
          customRender: function (row) {
            if (row && row.value && row.value.respirationRate) {
              return row.value.respirationRate;
            } else {
              return '';
            }
          }
        },
        {
          title: '心跳(次/min)',
          dataIndex: 'cattleRealtimeDataDTO',
          customRender: function (row) {
            if (row && row.value && row.value.heartRate) {
              return row.value.heartRate;
            } else {
              return '';
            }
          }
        },
        {
          title: '速率状态',
          dataIndex: 'cattleRealtimeDataDTO',
          customRender: function (row) {
            if (row && row.value && row.value.rateState) {
              if (row.value.rateState == 0) {
                return "无目标";
              } else if (row.value.rateState == 1) {
                return "信号微弱";
              } else if (row.value.rateState == 2) {
                return "呼吸正常";
              } else if (row.value.rateState == 3) {
                return "目标异常移动";
              }
            } else {
              return '';
            }
          }
        },
        {
          title: '躺卧状态',
          dataIndex: 'lieType',
          customRender: function (row) {
            if (row.text == 'liedown') {
              return '躺卧';
            } else if (row.text == 'stand') {
              return '站立';
            } else  {
              return '未知';
            }
          }
        },
        {
          title: '牧场',
          dataIndex: 'ranchName'
        },
        {
          title: '牛舍',
          dataIndex: 'oxtallName'
        },
        {
          title: '区域',
          dataIndex: 'areaName'
        },
        {
          title: '牛群',
          dataIndex: 'herdName'
        },
        {
          title: '出生日期',
          dataIndex: 'birthday'
        },
        {
          title: '牛龄(天)',
          dataIndex: 'birthday',
          customRender: function(row) {
            if (row.text != null && row.text != '') {
              return moment().diff(moment(row.text), 'day');
            } else {
              return '-';
            }
          }
        },
        {
          title: '耳标号1',
          dataIndex: 'earOne'
        },
        {
          title: '耳标号2',
          dataIndex: 'earTwo'
        },
        {
          title: '围栏',
          dataIndex: 'fence'
        },
        {
          title: '录入来源',
          dataIndex: 'inputSource',
          customRender: function(row) {
            if (row.text == 1) {
              return "后台录入";
            } else if (row.text == 2) {
              return "小程序(读写棒)录入";
            } else if (row.text == 3) {
              return "挤奶厅录入";
            }else {
              return "其他";
            }
          }
        },
        {
          title: '操作',
          key: 'action',
          width: 180,
          align: 'center',
          fixed: 'right'
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示按钮管理弹窗
      showHealthData: false,
      showDeviceData : false,
      showSportData : false,
      showLiedownData : false,
      showEstrusData : false,
      showStomachData : false,
      ranchList : [],
      oxtallList : [],
      herdList : []
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2023/03/18 20:10
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2023/03/18 20:10
     */
    reset() {
      this.where.cattleNo = undefined;
      this.where.oxtId = '';
      this.where.ranchId = ''
      this.where.herdId = ''
      this.where.thermNo = ''
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2023/03/18 20:10
     */
    async remove(row) {
      const result = await CattleApi.delete({ cattleId: row.cattleId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2023/03/18 20:10
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /**
     * 打开区域管理对话框
     *
     */
    openTemplateDataDialog(row) {
      this.current = row;
      this.showHealthData = true;
    },
    openDeviceDataDialog(row) {
      this.current = row;
      this.showDeviceData = true;
    },
    openSportDataDialog(row) {
      this.current = row;
      this.showSportData = true;
    },
    openLiedownDataDialog(row) {
      this.current = row;
      this.showLiedownData = true;
    },
    openEstrusDataDialog(row) {
      this.current = row;
      this.showEstrusData = true;
    },
    openStomachDataDialog(row) {
      this.current = row;
      this.showStomachData = true;
    },

    datasource({ page, limit, where, orders }) {
      return CattleApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    },

    selectRanch(ranchId) {
      this.oxtallList = [];
      this.where.oxtId = undefined;
      OxtallApi.list({"orgId" : ranchId}).then(result => {
        this.oxtallList = (result ? result.data : null) || [];
      }).catch((err) => {
        console.log(err)
      });
    },
  }
};
</script>
